package cn.zyjblogs.server.user.service.impl;

import cn.zyjblogs.server.user.po.OauthUserDetails;
import cn.zyjblogs.server.user.po.UserPo;
import cn.zyjblogs.server.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author zhuyijun
 */
@Service("userDetailsService")
@RequiredArgsConstructor
public class OauthUserDetailsServiceImpl implements UserDetailsService {
    private final UserService userService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<UserPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserPo::getUsername,s).or().eq(UserPo::getEmail,s).or().eq(UserPo::getPhone,s);
        UserPo userPo = userService.getBaseMapper().selectOne(queryWrapper);
        if (userPo == null){
            return null;
        }
        OauthUserDetails oauthUserDetails = new OauthUserDetails();
        BeanUtils.copyProperties(userPo, oauthUserDetails);
        return oauthUserDetails;
    }
}
